package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomProblemMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Problem;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.User;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.MessageImpartVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.activiti.base.WorkFlowService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomProblemService;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomUserService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.OutMessageImpartService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 问题管理service实现类
 * Created by liuyj on 2017/1/12.
 */
public class SomProblemServiceImpl implements SomProblemService {
    public static final String PROBLEM_FLOW_KEY = "problem";
    @Autowired
    private SomProblemMapper somProblemMapper;
    @Autowired
    private SomFileMapper somFileMapper;
    @Autowired
    private OutMessageImpartService outMessageImpartService;
    @Autowired
    private SomUserService userService;
    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 新增问题管理信息
     *
     * @param problem
     * @return
     */
    @Override
    public int addProblem(Problem problem) {
        List<File> files = problem.getFiles();
        if (files != null && files.size() > 0) {
            for (File f : files) {
                f.setBusinessId(problem.getProblemId());
                f.setFileId(UniqueUtil.uuid());
                somFileMapper.insert(f);
            }
        }
        // 上报人信息
        User user = userService.loginByUserId(problem.getCreateUserId());
        String content = "";
        if (user != null) {
            content = user.getUserName() + "于" + DateUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm") + "上报问题，请处置！";
        }
        // 上报问题时推送消息
        List<User> users = userService.getDdsUsers();
        if (users != null && users.size() > 0) {
            MessageImpartVo messageImpartVo = new MessageImpartVo();
            messageImpartVo.setMessageContent(content);
            String userIds = "";
            for (User u : users) {
                userIds += u.getUserId() + ",";
            }
            if (userIds.length() > 1) {
                userIds = userIds.substring(0, userIds.length() - 1);
            }
            messageImpartVo.setUserId(userIds);
            messageImpartVo.setMessageSource((short) 1);
            outMessageImpartService.saveMessageImpart(messageImpartVo);
        }
        problem.setFindTimeDate(new Date(problem.getFindTime()));
        String depName = null != user.getDeptName() && user.getDeptName() != "" ? user.getDeptName() : user.getOrgName();
        if (depName.contains("净化")) {
            problem.setProblemSource(1);
        } else if (depName.contains("维护")) {
            problem.setProblemSource(2);
        }
        //发起流程
        try {
            workFlowService.startProcessInstanceByKey(PROBLEM_FLOW_KEY, problem.getProblemId(), null, problem.getCreateUserId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return somProblemMapper.insert(problem);
    }
}
